package de.derhess.projects.pkr.controller
{
	import de.derhess.projects.pkr.models.LawyerCost;
	import de.derhess.projects.pkr.models.LawyerInstanceCost;
	
	import mx.events.PropertyChangeEvent;

	/**
	 * released under MIT License (X11)
	 * http://www.opensource.org/licenses/mit-license.php
	 * 
	 * Class  ControllerLawyerInstanceCost
	 * 
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 */
	public class ControllerLawyerCost 
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		public static var PROPERTY_CHANGE_VAT_SUM:String = "vatSum";
		public static var PROPERTY_CHANGE_SUM:String = "sum";
		
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var model:LawyerCost;
		
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function ControllerLawyerCost(data:LawyerCost)
		{
			model = data;
			
			controllerInstance = new ControllerLawyerInstanceCost(this.model.instance1);
			controllerAppeal1 = new ControllerLawyerInstanceCost(this.model.appeal1);
			controllerAppeal2 = new ControllerLawyerInstanceCost(this.model.appeal2);
	
			this.model.instance1.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleChange);
			this.model.appeal1.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleChange);
			this.model.appeal2.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleChange);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		public var controllerInstance:ControllerLawyerInstanceCost;
		public var controllerAppeal1:ControllerLawyerInstanceCost;
		public var controllerAppeal2:ControllerLawyerInstanceCost;
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		public function setLawyerFee(value:Number):void
		{
			controllerInstance.setLawyerFee(value);
			controllerAppeal1.setLawyerFee(value);
			controllerAppeal2.setLawyerFee(value);
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		public function toString():String 
		{
			return "[ControllerLawyerCost] : _model: " + model.toString();	
		}
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		public function setStartValues(lawyerFee:Number = 0, vatValue:int = 19):void
		{
			controllerInstance.setStartValues(lawyerFee,vatValue);
			controllerAppeal1.setStartValues(lawyerFee,vatValue);
			controllerAppeal2.setStartValues(lawyerFee,vatValue);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handleChange(e:PropertyChangeEvent):void
		{
			this.model.sum = this.model.instance1.sum + this.model.appeal1.sum + this.model.appeal2.sum;
			this.model.vatSum = this.model.instance1.vat.sum + this.model.appeal1.vat.sum + this.model.appeal2.vat.sum;
		}

		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}